package com.tanhua.server.controller;

import com.tanhua.domain.mongo.Publish;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 圈子动态
 */
@RestController
@RequestMapping("/movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    /**
     * 推荐动态
     * GET/movements/recommend
     */
    @GetMapping("/recommend")
    public ResponseEntity recommendPublish(@RequestParam(defaultValue = "1") int page,
                                           @RequestParam(defaultValue = "10") int pagesize) {
        return movementsService.findRecommendPublish(page, pagesize);
    }

    /**
     * 发布动态
     * POST/movements
     */
    @PostMapping
    public ResponseEntity publishMovement(MultipartFile[] imageContent,
                                          String location,
                                           Publish publish) throws IOException {
        publish.setLocationName(location);
        return movementsService.publishMovement(imageContent, publish);
    }

    /**
     * 好友动态
     * GET/movements
     */
    @GetMapping
    public ResponseEntity publishOfFriend(@RequestParam(defaultValue = "1") int page,
                                          @RequestParam(defaultValue = "10") int pagesize) {
        return movementsService.findPublishOfFriend(page, pagesize);
    }

    /**
     * 用户动态
     * GET/movements/all
     */
    @GetMapping("/all")
    public ResponseEntity userPublish(@RequestParam(defaultValue = "1") int page,
                                      @RequestParam(defaultValue = "10") int pagesize,
                                      @RequestParam(required = false) Long userId) {
        if (userId == null) {
            userId = UserHolder.getUser().getId();
        }
        return movementsService.findUserPublish(page, pagesize, userId);
    }

    /**
     * 动态-点赞
     * GET/movements/:id/like
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String publishId) {
        return movementsService.like(publishId);
    }

    /**
     * 动态-取消点赞
     * GET/movements/:id/dislike
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String publishId) {
        return movementsService.dislike(publishId);
    }

    /**
     * 动态-喜欢
     * GET/movements/:id/love
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String publishId) {
        return movementsService.love(publishId);
    }

    /**
     * 动态-取消喜欢
     * GET/movements/:id/unlove
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unLove(@PathVariable("id") String publishId) {
        return movementsService.unLove(publishId);
    }

    /**
     * 单条动态
     * GET/movements/:id
     * 注意：单条动态写完会看不到效果，只有写完评论列表才能看到
     */
    @GetMapping("/{id}")
    public ResponseEntity findOnePublish(@PathVariable("id") String publishId) {
        return movementsService.findOnePublish(publishId);
    }

    /**
     * 最近访客
     * GET/movements/visitors
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors() {
        return movementsService.findVisitors();
    }
}
